package com.travel.server.manager.impl.command.adversiting;

import com.travel.common.CommonConstant;
import com.travel.server.manager.dto.request.advertising.UpdateAdvertisingPositionRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.PositionDal;
import com.travel.server.manager.model.AdvertisingPositionInfo;
import com.travel.server.manager.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/20 15:25
 * @Desc:
 */
@Component
public class UpdateAdvertisingPositionCommand extends AbstractCommand<UpdateAdvertisingPositionRequest, EmptyResponse> {

    @Autowired
    private PositionDal positionDal;

    @Override
    protected void checkInput(UpdateAdvertisingPositionRequest request) {
    }

    @Override
    public EmptyResponse handle(UpdateAdvertisingPositionRequest request) {
        UserInfo userInfo = getUserInfo();
        Date now = new Date();
        AdvertisingPositionInfo info;
        if(request.getId() == null){
            info = new AdvertisingPositionInfo();
            info.setCreateBy(userInfo.getName());
            info.setCreateTime(now);
            info.setState(CommonConstant.YES);
        }else{
            info = positionDal.selectById(request.getId());
        }
        info.setPositionDescribe(request.getDesc());
        info.setPositionHeight(request.getHeight());
        info.setPositionName(request.getName());
        info.setPositionWeight(request.getWeight());
        info.setUpdateBy(userInfo.getName());
        info.setUpdateTime(now);
        positionDal.insertOrUpdate(info);
        return new EmptyResponse();
    }
}
